//
//  PywLoginApi.m
//  PywSdk
//
//  Created by Jade on 16/7/30.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywLoginApi.h"
#import "AIFLoginManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywResourceUtils.h"
#import "PropertyListReformer.h"
#import "PywUser.h"
#import "PywPassportUtils.h"
#import "PywAccount.h"

@interface PywLoginApi()<ETAPIManagerCallBackDelegate>
@end
@implementation PywLoginApi

- (void)requestLoginWithParams:(MutableDictionary *)params
                       success:(Successd)success
                          fail:(Failure)fail
{
    self.successd = success;
    self.failure = fail;
    [PywMBProgressHUD showMessage:@"正在登录..."];
    AIFLoginManager *loginManager = [AIFLoginManager manager];
    loginManager.passport = [params objectForKey:@"name"];
    loginManager.password = [params objectForKey:@"password"];
    loginManager.isEncryptRSA = [[params objectForKey:@"isEncryptRSA"] boolValue];
    loginManager.delegate = self;
    [loginManager loadData];
}

#pragma mark - ETAPIManagerCallBackDelegate
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUD];
    [self loginPassportFail];
}

/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUD];
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [self loginPassportSuccess:dict];
    
}

/**
 *  登录平台账号失败
 */
- (void)loginPassportFail
{
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

/**
 *  登录平台账号成功
 */
- (void)loginPassportSuccess:(NSDictionary *)dict
{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    
    switch (resultCode) {
        case 200:
        {
            NSArray *gameCount = dict[@"data"][@"game_account"];
            
            NSMutableArray *accountList = [[NSMutableArray alloc] initWithCapacity:gameCount.count];
            for (NSDictionary *dict in gameCount) {
                PywAccount *account = [[PywAccount alloc] init];
                account.account = dict[@"account"];
                account.cp_uid = dict[@"cp_uid"];
                [accountList addObject:account];
            }

            /**
             *  登录成功回调
             */
            NSString *account_type = [self savePassportInfo:dict];
            
            if (self.successd) {
                self.successd(account_type,accountList);
            }
        }
            break;

        default:
        {
            NSString *errorMsg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:errorMsg toView:[UIApplication sharedApplication].keyWindow];
            if (self.failure) {
                self.failure([self errorWithCode:resultCode]);
            }
        }
            break;
    }
}

/**
 *  保存账号信息
 *
 *  @param dict account_Info
 *
 *  @return account_type
 */
- (NSString *)savePassportInfo:(NSDictionary *)dict
{
    // 保存用户模型
    PywUser *user = [[PywUser alloc] init];
    user.passport = dict[@"data"][@"username"];
    user.password = dict[@"data"][@"password"];
    user.accountType = [NSString stringWithFormat:@"%@",dict[@"data"][@"account_type"]];
    [PywPassportUtils savePassport:user];
    
    // 保存登录的状态
    [PywPlatformMain sharedPlatformMain].logined = YES;
    [PywPlatformMain sharedPlatformMain].logouted = NO;
    
    //全局字段保存
    [PywPlatformMain sharedPlatformMain].accountType = [[NSString stringWithFormat:@"%@",dict[@"data"][@"account_type"]] integerValue];
    [PywPlatformMain sharedPlatformMain].userName = [NSString stringWithFormat:@"%@",dict[@"data"][@"username"]];
    [PywPlatformMain sharedPlatformMain].msisdn = [NSString stringWithFormat:@"%@",dict[@"data"][@"msisdn"]];
    [PywPlatformMain sharedPlatformMain].uid = [NSString stringWithFormat:@"%@",dict[@"data"][@"cp_uid"]];
    [PywPlatformMain sharedPlatformMain].is_verify_show = [[NSString stringWithFormat:@"%@",dict[@"data"][@"id_verify_show"]] boolValue];

    /**
     *  存储是否已经设置 交易密码
     */
    NSMutableDictionary *data = [dict objectForKey:@"data"];
    [PywPlatformMain sharedPlatformMain].is_have_pay_pwd = [[data objectForKey:@"have_pay_pwd"] boolValue];
    
    if ([PywPlatformMain sharedPlatformMain].accountType == UserType_Passport) {//平台账号登陆
        [PywPlatformMain sharedPlatformMain].passport = [PywPlatformMain sharedPlatformMain].userName;
        [PywPlatformMain sharedPlatformMain].passport_pwd = [NSString stringWithFormat:@"%@",dict[@"data"][@"password"]];
        NSString *passport_token = dict[@"data"][@"passport_token"];
        if (![passport_token isEqual:[NSNull null]]) {
            [PywPlatformMain sharedPlatformMain].passport_Token = [NSString stringWithFormat:@"%@",passport_token];
        }
    }else {//游戏账号登陆
        [PywPlatformMain sharedPlatformMain].account = [PywPlatformMain sharedPlatformMain].userName;
        [PywPlatformMain sharedPlatformMain].account_pwd = [NSString stringWithFormat:@"%@",dict[@"data"][@"password"]];
        [PywPlatformMain sharedPlatformMain].token = [NSString stringWithFormat:@"%@",dict[@"data"][@"token"]];
        //绑定通行证以后返回passport
        NSString *passport = [NSString stringWithFormat:@"%@",dict[@"data"][@"passport"]];
        if (passport.length > 0) {
            [PywPlatformMain sharedPlatformMain].passport = passport;
        }
    }
    
    
    NSString *account_type = user.accountType;
    return account_type;
}

@end
